//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Net;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.IPBanning.Components;

namespace CommunityServer.ControlPanel.Tools
{
	/// <summary>
	/// Summary description for ManageIPAddresses.
	/// </summary>
	public class ManageIPAddresses : BaseToolsPage
	{

		#region Members

		protected Repeater BannedIPS;
		protected TextBox IPStart;
		protected TextBox IPEnd;
		protected ResourceLinkButton AddButton;
		protected Label StatusMessage;
		protected CommunityServer.Controls.MPContent DescriptionRegion;
		protected CommunityServer.Controls.MPContent TaskRegion;
		protected CommunityServer.Controls.MPContainer MPContainer;
		protected CommunityServer.Controls.StatusMessage Status;
		protected CommunityServer.ControlPanel.Controls.ResourceControl RegionTitle;
		
		int i = 1;

		#endregion

		override protected void OnInit(EventArgs e)
		{
			if (!Telligent.Registration.CommunityServer.IsStandard)
			{
				Status.IsLicenseMessage = true;
				Status.Success = false;
				Status.Text = CommunityServer.ControlPanel.Components.ResourceManager.GetString("CP_Tools_ManageIP_LicenseNotAvailable");
				Status.Visible = true;
			}
			
			BannedIPS.ItemDataBound +=new RepeaterItemEventHandler(BannedIPS_ItemDataBound);
			BannedIPS.ItemCommand +=new RepeaterCommandEventHandler(BannedIPS_ItemCommand);
			AddButton.Click +=new EventHandler(AddButton_Click);

			this.Load += new EventHandler(this.Page_Load);
			
			base.OnInit(e);
		}

		private void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack) 
			{
				this.Bind();
			}
		}

		private void Bind()
		{
			BannedIPS.DataSource = IPBanningData.Merge();
			BannedIPS.DataBind();
		}


		#region Events

		private void BannedIPS_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			switch ( e.Item.ItemType ) 
			{
				case ListItemType.Item:
				case ListItemType.AlternatingItem:
				case ListItemType.SelectedItem:

					Literal ip = e.Item.FindControl("IP") as Literal;

					BannedAddress ba = e.Item.DataItem as BannedAddress;
					ip.Text = i.ToString() + ". " + ba.ToString();
					i++;

					ResourceLinkButton btn = e.Item.FindControl("DeleteButton") as ResourceLinkButton;
					btn.CommandName = e.Item.DataItem.GetType().ToString();
					btn.CommandArgument = ba.Key;
					btn.Attributes.Add("onclick", string.Format("return confirm('Are you sure you want to delete {0}?');",ba.ToString()));
				
					break;
			}
		}

		private void BannedIPS_ItemCommand(object source, RepeaterCommandEventArgs e)
		{
			
			if(e.CommandName == typeof(BannedAddress).ToString())
			{
				BannedAddress ba = new BannedAddress();
				ba.Address = e.CommandArgument.ToString();
				ba.Date = DateTime.Now;
				ba.SettingsID = CSContext.Current.SiteSettings.SettingsID;
				IPBanningData.DeleteBannedAddress(ba);
				Bind();
			}
			else if (e.CommandName == typeof(BannedNetwork).ToString())
			{
				BannedNetwork network = new BannedNetwork();
				network.BannedID =  Int32.Parse(e.CommandArgument.ToString());
				network.Date = DateTime.Now;
				network.SettingsID = CSContext.Current.SiteSettings.SettingsID;
				IPBanningData.DeleteBannedNetwork(network);

				Bind();
			}
			
		}

		private void InitializeComponent()
		{
		
		}

		private void AddButton_Click(object sender, EventArgs e)
		{
			IPAddress start = null;
			IPAddress end = null;
			if(IPStart.Text.Trim().Length == 0)
				return;

			try
			{
				start = IPAddress.Parse(IPStart.Text);
			}
			catch
			{
				StatusMessage.Text = "Invalid IP Format " + IPStart.Text;
				return;
			}

			if(IPEnd.Text.Trim().Length > 0)
			{
				try
				{
					end = IPAddress.Parse(IPEnd.Text);
				}
				catch
				{
					StatusMessage.Text = "Invalid IP Format " + IPEnd.Text;
					return;
				}
			}

			BannedNetwork bn = new BannedNetwork();
			bn.SettingsID = CSContext.Current.SiteSettings.SettingsID;
			bn.Address = IPStart.Text;
			bn.Date = DateTime.Now;

			if(end != null)
			{
				bn.EndAddress = IPEnd.Text;

				IPBanningData.AddBannedNetwork(bn);
			}
			else
			{
				IPBanningData.AddBannedAddress(bn);
			}

			//Context.Response.Redirect(Context.Request.Path);

			Bind();
		}

		#endregion

	}
}
